import 'vux/src/styles/reset.less';
import './had_to_borrow_books.scss'
import {
  Badge,
  Group,
  Cell,
  Panel,
  XButton,
  Scroller,
  Spinner
} from 'vux'

import { fetch, rap } from 'js/fetch.js'
let url = {
  list: '/merchandiseHot/list.do',
  slideList: '/slide/listSlides.do',
  postCollectBook: 'api/books',
  getBorrowList: 'api/book/borrows'
}
url = rap(url)

import utils from 'js/utils.js'

import { cookie } from 'vux'


import Booklist from 'components/booklist/booklist.vue'

import mixin from 'js/mixin.js'

new Vue({
  el: '#body',
  data: {
    haveData:false,//是否有数据
    showList1: true,
    lists: [
      {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      }, {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      },
      {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      },
      {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      },
      {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      },
      {
        title: '婴儿画报2017年第三季度合订本',
        img: '/static/book.jpg',
        author: '作者金波',
        status: '已领取',
      },
    ],
    bookList: [],
    limit: 5,//取几个
    offset: 0,//从第几个开始取,
    stop: false,//停止加载
    scrollTop: 0,
    onFetching: false,
    bottomCount: 20,
    n4: 10,
    demo4Value: {
      pullupStatus: 'default'
    }
  },
  created() {
    this.isLogin = utils.isLogin()
    //没有登录去登录
    if (!this.isLogin) {
      window.location.href = './login.html'
    } else {
      this.token = cookie.get('token')
      this.getCollectionBook()
    }
    //http://www.5chelib.com/api/book/borrows
  },
  methods: {
    load() {
      //子级触发父级加载数据事件

      //增加翻页标志
      this.offset += this.limit
      //请求数据
      this.getCollectionBook()
    },
    show(index) {
      if (this.listIndex > index) {
        this.transitionName = 'slide-right'
      } else {
        this.transitionName = 'slide-left'
      }
      this.listIndex = index
    },
    getCollectionBook() {
      //收藏图书列表
      if (!!this.stop) {
        return
      }

      fetch('get', `${url.getBorrowList}?limit=${this.limit}&offset=${this.offset}&type=borrow`, {}, { 'headers': this.token }).then(res => {
        if (res.status >= 200 && res.status <= 300) {
          
          res.data.forEach(element => {
            //console.log(element)
            for (let key in element.book) {
              //如果没有这个key就添上
              if(!element[key]){
                element[key]=element.book[key]
                //console.log(key)
              }
            }
          });
          this.haveData=true

          //console.log(res.data)
          this.bookList = this.bookList.concat(res.data)
          console.log(this.bookList)
          if (res.data.length == 0) {
            //没有数据就停止懒加载
            this.stop = true
          }
          if (!!this.$refs.booklist) {
            //兼容报错
            this.$refs.booklist.load()
          }
        } else {
          this.$vux.toast.show({
            text: res.data.message,
            type: 'warn',
            onShow() {
              //console.log('Plugin: I\'m showing')
            },
            onHide() {
              //console.log('Plugin: I\'m hiding')
            }
          })
        }
      })
    }
  },
  components: {
    Spinner,
    Badge,
    Group,
    Cell,
    Panel,
    XButton,
    Scroller,
    Booklist
  },
  mixins: [mixin]
})
